<?php

class Doctrine_Model_SucursalDao extends Doctrine_Model_BaseDao {
    
    public function obtenerTodos() {
        return $this->getEntityManager()
                ->createQuery('select t from Doctrine_Model_Entities_Sucursal t')
                ->getResult();
    }
     public function obtenerTodosNegocios() {
        return $this->getEntityManager()
                ->createQuery('select t from Doctrine_Model_Entities_Sucursal t where t.negocio=1')
                ->getResult();
    }

    public function obtenerPorId($id) {
        return $this->getEntityManager()->find('Doctrine_Model_Entities_Sucursal', $id);
    }
    
    public function guardar(Doctrine_Model_Entities_Sucursal $sucursal) {
        try {        
            $this->getEntityManager()->persist($sucursal);
            $this->getEntityManager()->flush();
            return true;
        } catch (Exception $e) {
            $logger = Zend_Registry::get('logger');
            $logger->log("Error Sucursal: " . $e->getMessage(), Zend_Log::ERR);
            return false;
        }
    }

    public function eliminar(Doctrine_Model_Entities_Sucursal $sucursal) {
        try {
            $this->getEntityManager()->remove($sucursal);
            $this->getEntityManager()->flush();
            return true;
        } catch (Exception $e) {
            $logger = Zend_Registry::get('logger');
            $logger->log("Error Sucursal: " . $e->getMessage(), Zend_Log::ERR);
            return false;
        }
    }

    public function toArrayCollection($coleccion) {
        $arrayCollection = new \Doctrine\Common\Collections\ArrayCollection();
        if (is_array($coleccion)) {
            foreach ($coleccion as $objeto) {
                $arrayCollection->add($objeto);
            }
        } else {
            if ($coleccion != NULL) {
                $arrayCollection->add($coleccion);
            }
        }
        return $arrayCollection;
    }
    
}
